package com.qdlc.p2p.manage.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ScoreConvertService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.ScoreConvert;
import com.qdlc.p2p.dal.model.ScoreConvertModel;

/**
 * 积分兑换信息管理
 * @author xxx
 * @version 2.0
 * @date 2015年6月15日 下午5:59:10
 */
public class ManageScoreConvertAction extends BaseAction<ScoreConvertModel> implements ModelDriven<ScoreConvertModel> {

	@Resource
	private ScoreConvertService scoreConvertService;
	
	private Map<String, Object> data;
	
	/**
	 * 
	 * 跳转积分兑换列表页          
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/user/score/convert/manage")
	public String manage() throws Exception {
		return "manage";
	}
	
	/**
	 * 积分兑换分页列表                 
	 * @throws Exception
	 */
	@Action("/modules/user/score/convert/list")
	public void list() throws Exception{
		if(StringUtil.isBlank(paramString("status"))){
			model.setStatus(99);
		}
		PageDataList<ScoreConvertModel> page = scoreConvertService.findPageList(model);
		data = new HashMap<String, Object>();
		data.put("total", page.getPage().getTotal());
		data.put("rows", page.getList());
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 积分兑换审核
	 * @return
	 */
	@Action("/modules/user/score/convert/verify")
	public String verify(){
		ScoreConvert item = scoreConvertService.getScoreConvertById(model.getId());
		request.setAttribute("item", item);
		request.setAttribute("goods", item.getGoods());
		request.setAttribute("user", item.getUser());
		return "verify";
	}
	
	/**
	 * 处理积分兑换审核
	 * @throws Exception
	 */
	@Action("/modules/user/score/convert/doVerify")
	public void doVerify() throws Exception {
		model.setVerifyOperator(this.getOperator());
		Boolean result = scoreConvertService.verifyScoreConvert(model);
		if (result) {
			printResult(MessageUtil.getMessage("I10009"), result);
		} else {
			printResult(MessageUtil.getMessage("I10019"), result);
		}
	}
	
	/**
	 * 发货
	 * @throws Exception
	 */
	@Action("/modules/user/score/convert/delivery")
	public String delivery() throws Exception{
		ScoreConvert item = scoreConvertService.getScoreConvertById(model.getId());
		request.setAttribute("item", item);
		request.setAttribute("goods", item.getGoods());
		request.setAttribute("user", item.getUser());
		return "delivery";
	}
	
	/**
	 * 处理发货
	 * @throws Exception
	 */
	@Action("/modules/user/score/convert/doDelivery")
	public void doDelivery() throws Exception{
		ScoreConvert item = scoreConvertService.getScoreConvertById(model.getId());
		item.setCourierName(model.getCourierName());
		item.setCourierNum(model.getCourierNum());
		item.setDeliverTime(new Date());
		item.setStatus(ScoreConvertModel.IS_DELIVERY);
		scoreConvertService.update(item);
		printSuccess();
	}
	
}
